package com.thti.mecool.client.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

import com.threeti.mecool.mobile.interfaces.dto.CommunicationDto;
import com.thti.mecool.client.adapter.CommunicationShowAdapter;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.command.send.communication.CommunicationSend;
import com.thti.mecool.client.model.biz.CommunicationBizImpl;
import com.thti.mecool.client.model.po.Communication;
import com.thti.mecool.client.model.po.CommunicationItem;
import com.thti.mecool.client.protocol.MessageInfo;
import com.thti.mecool.client.protocol.Page;
import com.thti.mecool.client.protocol.communication.CommunicationResult;
import com.thti.mecool.client.util.Util;
import com.thti.mecool.client.widget.MyListView;
import com.thti.mecool.client.widget.ProgressDialogWidget;
import com.thti.mecool.client.widget.Toolbar;

public class CommunicationActivity extends BaseActivity{
	private MyListView listView;
	List<CommunicationItem> list;
	public static Handler handler;
	private ProgressDialogWidget progressDialogWidget;
	CommunicationResult communicationResult;
	CommunicationBizImpl communicationBizImpl;
	List<Communication> communications;
	CommunicationShowAdapter adapter;
	private Page whichPage;
	private int page=1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.noTitle();
		setContentView(R.layout.mecool_communication);
		Cache.add(this);
		initToolbar();
		init();
		handler=new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				Bundle bundle=msg.getData();
				if(msg.what==0xCD){
					progressDialogWidget.dismiss();
					if((whichPage.getPage()+1)==1){
						communications.clear();
					}
					communications.addAll(communicationBizImpl.findAll());
					adapter=new CommunicationShowAdapter(CommunicationActivity.this, communications);
					listView.setAdapter(adapter);
					CommunicationResult communicationResult=(CommunicationResult)msg.obj;
					whichPage=communicationResult.getPage();
					Util.sendToast(CommunicationActivity.this, "第"+(whichPage.getPage()+1)+"页");
					page++;
				}else if(msg.what==0xDD){
					if(whichPage.isHasNext()){
						listView.onRefreshComplete();
					}
					MessageInfo messageinfo=(MessageInfo)bundle.getSerializable("msginfo");
					progressDialogWidget.dismiss();
					isRequestServerSuccess(messageinfo);
				}else if(msg.what==0xDE){
					listView.onRefreshComplete();
					CommunicationResult communicationResult=(CommunicationResult)msg.obj;
					communications.addAll(getCommunications(communicationResult.getCommunications()));
					adapter.notifyDataSetChanged();
					whichPage=communicationResult.getPage();
					Util.sendToast(CommunicationActivity.this, "第"+(whichPage.getPage()+1)+"页");
					page++;
				}
			}
			
		};
	   service();
	}
	
	private void init(){
		listView=(MyListView)findViewById(R.id.mecool_communication_listview);
		communicationBizImpl=new CommunicationBizImpl(this);
		whichPage=new Page();
	}
	
	private void service(){
		communications=communicationBizImpl.findAll();
		adapter=new CommunicationShowAdapter(CommunicationActivity.this, communications);
		listView.setAdapter(adapter);
		listView.setonRefreshListener(refreshListener);
		page=1;
		requestToServer(page);
	}
	
	com.thti.mecool.client.widget.MyListView.OnRefreshListener refreshListener=new com.thti.mecool.client.widget.MyListView.OnRefreshListener() {
		
		@Override
		public void onRefresh() {
			// TODO Auto-generated method stub
			if(whichPage.isHasNext()){
				requestToServer(page);
			}else{
				listView.onRefreshComplete();
				Util.sendToast(CommunicationActivity.this, "已是最后一页");
			}
		}
	};
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	
	public void initToolbar(){
		Toolbar toolbar=new Toolbar(this);
		toolbar.setToolbarCentreText("沟通管理");
		Toolbar.left_btn.setOnClickListener(leftbuttonListener);
		Toolbar.right_btn.setOnClickListener(rightbuttonListener);
		Toolbar.right_btn.setImageResource(R.drawable.mecool_add_communication_mark);
		Toolbar.left_btn.setImageResource(R.drawable.mecool_go_back);
	}
	private OnClickListener leftbuttonListener=new ImageButton.OnClickListener(){

		@Override
		public void onClick(View v) {
			finish();
		}};
		
	private OnClickListener rightbuttonListener=new ImageButton.OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent=new Intent(CommunicationActivity.this,AddCommicationRecordActivity.class);
			startActivityForResult(intent, 10);
		}
		
	};
	
	private void requestToServer(int pageNum){
		CommunicationSend.searchCommunication(BaseActivity.getCurrentUser().getLoginName(), pageNum, 10);
		if(pageNum==1){
			progressDialogWidget=new ProgressDialogWidget(this);
			progressDialogWidget.setDismissTime(15);
		}
	}
	
	public void isRequestServerSuccess(MessageInfo messageInfo){
			Toast.makeText(CommunicationActivity.this, messageInfo.getContent(), 5000).show();
			
	}
	
	private List<Communication> getCommunications(List<CommunicationDto> communicationDtos){
		List<Communication> communications=new ArrayList<Communication>();
		if(communicationDtos.size()>0){
			for(CommunicationDto communicationDto:communicationDtos){
				Communication communication=new Communication();
				communication.setCommunicationId(communicationDto.getCommId());
				communication.setContactName(communicationDto.getContactName());
				communication.setContactPhoneOrOther(communicationDto.getContactPhoneOrOther());
				communication.setContactPosition(communicationDto.getContactPosition());
				communication.setContent(communicationDto.getContent());
				communication.setProjId(communicationDto.getProjId());
				communication.setProjStoreId(communicationDto.getProjStoreId());
				communication.setTitle(communicationDto.getTitle());
				communication.setType(communicationDto.getType());
				communication.setUserId(String.valueOf(BaseActivity.getCurrentUser().getId()));
				communication.setRecordDate(communicationDto.getRecordDate());
				communication.setProjName(communicationDto.getProjName());
				communication.setProjStoreName(communicationDto.getProjStoreName());
				communications.add(communication);
			}
		}
		return communications;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==10){
			if(requestCode==RESULT_OK){
				page=1;
				requestToServer(page);
			}
			
		}
	}
	
	
	
	
//	private void addMorePage(){
//		View view=LayoutInflater.from(this).inflate(R.layout.mecool_listview_footview, null);
//		moreTextView=(TextView)view.findViewById(R.id.mecool_listview_footview_moretextview);
//		loadProgressBarLinearlayout=(LinearLayout)view.findViewById(R.id.mecool_listview_footview_progressbar_linearlayout);
//		moreTextView.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				moreTextView.setVisibility(View.GONE);
//				loadProgressBarLinearlayout.setVisibility(View.VISIBLE);
//			}
//		});
//		listView.addFooterView(view);
//	}

	
	
	
	
	
}
